package com.link.play.fit.ai.ui.vip

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.android.billingclient.api.ProductDetails
import com.bumptech.glide.Glide
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.databinding.AcVipOffBinding
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.utils.GooglePayUtil
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VipOffAc : BaseActivity<AcVipOffBinding>(AcVipOffBinding::inflate) {
    private var subscriptionOfferDetails: List<ProductDetails.SubscriptionOfferDetails>? = null
    private var productDetails: ProductDetails? = null
    private var formattedPrice = ""
    private var yearFormattedPrice = ""


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)

        yearFormattedPrice = intent.getStringExtra("originalPrice").toString()

        Glide.with(this).asGif().load(R.drawable.hahaha).into(binding.ivGif)
        initListener()

        binding.tvBtn.setOnClickListener {
            "subscriptionOfferDetails $subscriptionOfferDetails".d()
            subscriptionOfferDetails?.forEach {
//                if (it.basePlanId == SUB_YEAR_DISCOUNT_PLAN_ID) {
//                    GooglePayUtil.pay(this, it.offerToken, productDetails!!)
//                }
            }
        }
    }


    @SuppressLint("DefaultLocale")
    private fun initListener(){
        binding.tvBtn.setOnClickListener {
            finish()
        }
//        lifecycleScope.launch {
//            productDetails = GooglePayUtil.queryProduct(SUB_YEAR_DISCOUNT_PRODUCT_ID)
//            subscriptionOfferDetails = productDetails?.subscriptionOfferDetails
//            productDetails?.subscriptionOfferDetails?.forEach {
//                val pricingPhase = it.pricingPhases.pricingPhaseList[0]
//                when (it.basePlanId) {
//                    SUB_YEAR_DISCOUNT_PLAN_ID -> {
//                         formattedPrice = pricingPhase.formattedPrice
//                        binding.tvPerMonth.text = getString(
//                            R.string.only_month,
//                            extractAndCalculateDailyPriceToMonth(formattedPrice)
//                        )
//                       val dem = PriceUtils.calculatePercentage(yearFormattedPrice, formattedPrice, 0)
//                        binding.tvOff.text = getString(
//                            R.string.text_off,
//                            dem
//                        )
//                    }
//
//                    else -> {}
//                }
//            }
//        }


    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: String) {
        if(event == "isVip"){
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}